using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace WebCrawlerTools.form
{
    class HElementFactory
    {
        public static HElement createElement(HtmlElement fele)
        {
            HElement hele = null;
            if (fele.TagName == "INPUT")
            {
                string type = fele.GetAttribute("type");
                if (type == "text")
                {
                    if (fele.GetAttribute("value") != "")
                    {
                        hele = new HInputText();
                        hele.ID = fele.Id;
                        hele.Name = fele.Name;
                        hele.Val = fele.GetAttribute("value");
                    }
                }
                //else if(type == "checkbox")
                //{
                //    hele = new HInputCheck();
                //    hele.ID = fele.Id;
                //    hele.Name = fele.Name;
                //    hele.Val = fele.GetAttribute("value");
                //}
                //else if(type == "radio")
                //{
                //    hele = new HInputRadio();
                //    hele.ID = fele.Id;
                //    hele.Name = fele.Name;
                //    hele.Val = fele.GetAttribute("value");
                //}
            }
            else if (fele.TagName == "SELECT")
            {
                if (fele.GetAttribute("value") != "")
                {
                    hele = new HSelectOne();
                    hele.ID = fele.Id;
                    hele.Name = fele.Name;
                    hele.Val = fele.GetAttribute("value");
                }
            }
            return hele;
        }

        public static HElementType getElementType(HtmlElement fele)
        {
            HElementType eletype = HElementType.UNKNOW;
            if (fele.TagName == "INPUT")
            {
                string type = fele.GetAttribute("type");
                if (type == "text")
                {
                    eletype = HElementType.TEXT;
                }
                else if (type == "checkbox")
                {
                    eletype = HElementType.CHECKBOX;
                }
                else if (type == "radio")
                {
                    eletype = HElementType.RADIO;
                }
                else if (type == "hidden")
                {
                    eletype = HElementType.HIDDEN;
                }
            }
            else if (fele.TagName == "SELECT")
            {
                eletype = HElementType.SELECT;
            }
            return eletype;
        }

        public static HForm createForm(HtmlElement form)
        {
            HForm hform = null;
            if (form != null)
            {
                hform = new HForm();
                HtmlElementCollection feleCol = form.All;
                hform.ID = form.Id;
                hform.Name = form.Name;
                foreach (HtmlElement fele in feleCol)
                {
                    HElement hele = createElement(fele);
                    if (hele != null)
                    {
                        hform.Elements.Add(hele);
                    }
                }
            }
            return hform;
        }

        public static void setForm(HtmlElement form, HForm hform)
        {
            if(hform!=null)
            {
                foreach (HElement he in hform.Elements)
                {
                    HtmlElement fele = getFormElementByName(form, he.Name);
                    if (fele != null)
                    {
                        if (he.Type == HElementType.TEXT)
                        {
                            fele.SetAttribute("value", he.Val);
                        }
                        else if (he.Type == HElementType.SELECT)
                        {
                            fele.SetAttribute("value", he.Val);
                        }
                    }
                }
            }
        }

        public static HtmlElement getFormElementByName(HtmlElement form,string name)
        {
            HtmlElementCollection feleCol = form.All;
            foreach (HtmlElement fele in feleCol)
            {
                HElementType type = getElementType(fele);
                if (type != HElementType.UNKNOW)
                {
                    if (fele.Name == name)
                    {
                        return fele;
                    }
                }
            }
            return null;
        }

        public static string getFormElementValue(HtmlDocument doc, string formName, string elementName)
        {
            string retVal = "";
            HtmlElementCollection forms = doc.Forms;
            foreach (HtmlElement form in forms)
            {
                if (formName == form.Name)
                {
                    HtmlElement formItem = getFormElementByName(form, elementName);
                    if (formItem != null)
                    {
                        retVal =formItem.GetAttribute("value");
                    }
                    break;
                }
            }
            return retVal;
        }
        public static HtmlElement getFormElement(HtmlDocument doc, string formName, string elementName)
        {
            HtmlElementCollection forms = doc.Forms;
            foreach (HtmlElement form in forms)
            {
                if (formName == form.Name)
                {
                    return getFormElementByName(form, elementName);
                }
            }
            return null;
        }
    }
}
